<?php

namespace app\common\util;

use app\admin\service\AdminPhoto;

class UploadUtil
{

    //通用上传
    public static function putFile($file, $type, $path, $save = 0, $local = false, $global_config = [], $backUrl = false)
    {
        $fileType = $global_config['file_type'] ?? 1;
        if (!$path) {
            $path = 'default';
        }
        try {
            if ($type == 'image') {
                $fileSize = isset($global_config['imgValidate_size']) ? $global_config['imgValidate_size'] * 1024 : config('procedure.uploadValidate.imgValidate.validate.size');
                $fileExt = implode(',', explode('|', $global_config['imgValidate_ext'] ?? config('procedure.uploadValidate.imgValidate.validate.ext')));
            } elseif ($type == 'video') {
                $fileSize = isset($global_config['videoValidate_size']) ? $global_config['videoValidate_size'] * 1024 : config('procedure.uploadValidate.videoValidate.validate.size');
                $fileExt = implode(',', explode('|', $global_config['videoValidate_ext'] ?? config('procedure.uploadValidate.videoValidate.validate.ext')));
            } elseif ($type == 'audio') {
                $fileSize = isset($global_config['audioValidate_size']) ? $global_config['audioValidate_size'] * 1024 : config('procedure.uploadValidate.audioValidate.validate.size');
                $fileExt = implode(',', explode('|', $global_config['audioValidate_ext'] ?? config('procedure.uploadValidate.audioValidate.validate.ext')));
            } else {
                $fileSize = isset($global_config['fileValidate_size']) ? $global_config['fileValidate_size'] * 1024 : config('procedure.uploadValidate.fileValidate.validate.size');
                $fileExt = implode(',', explode('|', $global_config['fileValidate_ext'] ?? config('procedure.uploadValidate.fileValidate.validate.ext')));
            }
            validate(['file' => [
                'fileSize' => $fileSize,
                'fileExt' => $fileExt,
            ]])->check(['file' => $file]);
        } catch (\think\exception\ValidateException $e) {
            return ['msg' => '上传失败', 'code' => 201, 'data' => $e->getMessage()];
        }
        foreach ($file as $k) {
            if ($backUrl) {
                $name = $k->getOriginalName();
                $savename = '/' . 'upload' . '/' . \think\facade\Filesystem::disk('public')->putFile($path, $k);
                $url = str_replace("\\", "/", $savename);
                return [dealImage($url), $name];
            }
            if ($fileType == 2 && !$local) {
                $name = $k->getOriginalName();
                //阿里云上传
                $res = OssUtil::alYunOSS($k, $k->extension(), 'moku/static_data/' . $path);
                if ($res["code"] == 201) {
                    return ['msg' => '上传失败', 'code' => 201, 'data' => [$res["msg"]]];
                }
                $url = $res['src'];
                if ($save) AdminPhoto::add($k, $url, $path, 2);
            } elseif ($fileType == 3 && !$local) {
                $name = $k->getOriginalName();
                //七牛上传
                $res = QiniuUtil::QiniuOSS($k, $k->extension(), $path);
                if ($res["code"] == 201) {
                    return ['msg' => '上传失败', 'code' => 201, 'data' => [$res["msg"]]];
                }
                $url = $res['src'];
                if ($save) AdminPhoto::add($k, $url, $path, 3);
            } else {
                $name = $k->getOriginalName();
                $savename = '/' . 'upload' . '/' . \think\facade\Filesystem::disk('public')->putFile($path, $k);
                $url = str_replace("\\", "/", $savename);
                if ($save) AdminPhoto::add($k, $url, $path, 1);
            }
        }
        return ['msg' => '上传成功', 'code' => 0, 'data' => ['src' => dealImage($url), 'thumb' => $name]];
    }
}
